﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SimplexEngine
{
    public class MRTPointLightMaterial : MRTLightMaterial
    {
        public MRTPointLightMaterial()
        {
            effect = Root.Instance.ContentSystem.GetResource("MRTPointLight.Shader");
            depth_texture.Create("MRT:DepthTexture", true);
        }

        protected override void OnApplyParameters(GameTime game_time, Int64 frame_number)
        {
            base.OnApplyParameters(game_time, frame_number);
            Effect.Parameters["DepthTexture"].SetValue(DepthTexture.Texture);

            RenderSystem.BasicRenderParameters render_parameters = Root.Instance.RenderSystem.RenderParameters;

            Matrix world = Matrix.CreateScale(range * 2.0f) * render_parameters.World;

            Effect.Parameters["ViewProjectionInverse"].SetValue(Matrix.Invert(render_parameters.View * render_parameters.Projection));
            Effect.Parameters["LightPosition"].SetValue(world.Translation);
            Effect.Parameters["LightRange"].SetValue(range);

            Effect.Parameters["ViewPortSize"].SetValue(new Vector2(Root.Instance.RenderSystem.ViewPort.Width, Root.Instance.RenderSystem.ViewPort.Height));
            ApplyBasicParameters(game_time, frame_number);
            Effect.Parameters["World"].SetValue(world);
        }

        public float Range { get { return range; } set { range = value; } }
        protected float range = 1.0f;

        public MaterialTexture DepthTexture { get { return depth_texture; } }
        protected MaterialTexture depth_texture = new MaterialTexture();

        public override Material Clone()
        {
            MRTPointLightMaterial material = new MRTPointLightMaterial();
            material.diffuse = diffuse;
            material.normal_texture = normal_texture;
            material.depth_texture = depth_texture;
            return material;
        }
    }
}
